<template>
  <el-dialog
    title="添加合伙人"
    :visible.sync="dialogVisible"
    width="600px"
  >
    <el-form
      ref="partnerForm"
      :model="partnerForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item
        label="合伙人姓名:"
        prop="partner_name"
      >
        <el-input
          v-model="partnerForm.partner_name"
          placeholder="请输入合伙人姓名"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="电话号码:"
        prop="partner_phone"
      >
        <el-input
          v-model="partnerForm.partner_phone"
          placeholder="请输入电话号码"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="备注:"
        prop="partner_desc"
      >
        <el-input
          v-model="partnerForm.partner_desc"
          type="textarea"
          placeholder="请输入备注信息"
        ></el-input>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="dialogVisible = false">关闭</el-button>
      <el-button
        type="primary"
        @click="submitForm"
      >
        保存
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import { apiCustomerPhoneCheck } from '@/api/clue'
import { isMobile } from '@/utils/validate'

export default {
  name: 'DialogPartner',
  data() {
    const validatePhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('电话号码不能为空'))
      }
      if (!isMobile(value)) {
        return callback(new Error('手机号格式错误'))
      }
      callback()
    }
    return {
      dialogVisible: false,
      partnerForm: {
        partner_name: '',
        partner_phone: '',
        partner_desc: ''
      },
      rules: {
        partner_phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    /**
     * 显示弹窗
     */
    showDialog() {
      this.dialogVisible = true
      this.partnerForm = {
        partner_name: '',
        partner_phone: '',
        partner_desc: ''
      }
    },
    /**
     * 提交表单
     */
    submitForm() {
      this.$refs.partnerForm.validate((valid) => {
        if (valid) {
          apiCustomerPhoneCheck({ phone: this.partnerForm.partner_phone }).then(
            (res) => {
              this.$emit('success', this.partnerForm)
              this.dialogVisible = false
            }
          )
        }
      })
    }
  }
}
</script>

<style scoped>
/* 可根据需求添加样式 */
</style>
